/**
 * 
 */
package info.niwota.profile.util;

import info.niwota.commons.web.JsonObject;
import info.niwota.profile.Preferences;
import info.niwota.profile.R;
import info.niwota.profile.client.HttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

/**
 * @author qiangli
 *
 */
public class MailTemplate {

	private static final int BUF_SIZE = 1024;

	private MailTemplate() {
	}
	
	public static void startSendActivity(Context ctx, String email, boolean inc) throws Exception {
		JsonObject p = Preferences.getProfileJSONObject(ctx, false);
		
		StringBuilder profile = new StringBuilder();
		
		final String name = p.getString(Preferences.PROFILE_NAME, "");
		final String id = Preferences.getProfileId(ctx, "");
		
		final String lf = ("\n");
		
		if (inc) {
			profile.append(name);
			profile.append(lf);
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_MOBILE, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_GMAIL, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_CALL, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_TEXT, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_CHAT, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_EMAIL, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_ADDRESS, ""));
			profile.append(lf);
			profile.append(p.getString(Preferences.PROFILE_WEBSITE, ""));
			profile.append(lf);
		}
		
		//
		String signature = p.getString(Preferences.PROFILE_SIGNATURE, "");
		
		final String url = profileLink(id, name);
		
		//
		String subject = TextUtils.isEmpty(name) ? ctx.getString(R.string.app_name) : ctx.getString(R.string.email_subject, name);
		
		//
		String tpl = readResource(ctx, R.raw.text);

		String body = MessageFormat.format(tpl, profile.toString(), signature, url);
	
		final String[] to = email == null ? null : new String[]{email};
		
		//
		Intent i = new Intent();
		i.setAction(Intent.ACTION_SEND);
		i.setType("text/plain"); //"message/rfc822"

		Intent chooser = Intent.createChooser(i, subject);
		chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		// email
		if (to != null) {
			i.putExtra(Intent.EXTRA_EMAIL, to);
		}
		i.putExtra(Intent.EXTRA_SUBJECT, subject);
		i.putExtra(Intent.EXTRA_TEXT, body); //
		
		//
		//if (uri != null) {
		//	i.putExtra(Intent.EXTRA_STREAM, uri);
		//}
		
		// mms
		if (email != null) {
			i.putExtra("address", email);
		}
		i.putExtra("exit_on_sent", true);
		i.putExtra("sms_body", body);
		
		//
		ctx.startActivity(Intent.createChooser(i, subject));
	}

	public static String profileLink(String id, String name) {
		//name is for display purpose only
		return Uri.parse(HttpClient.BASE_URL).buildUpon()
		.appendPath("profile").appendPath(id)
		.build().toString() + "#" + name.replaceAll("\\s", "_");
	}

	private static String readResource(Context context, int id) throws IOException {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(context
					.getResources().openRawResource(id)), BUF_SIZE);
			String line;
			StringBuilder sb = new StringBuilder();
			
			while ((line = in.readLine()) != null) {
				sb.append(line).append('\n');
			}
			
			return sb.toString();
		} finally {
			in.close();
		}
	}
}
